package com.api.custom.assets.formmode;

import com.alibaba.fastjson.JSONObject;
import com.api.formmode.cache.ModeComInfo;
import com.api.hg.scheld.GbSwExceptionConjob;
import com.api.hg.scheld.SwExceptionConjob;
import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import com.engine.common.util.ParamUtil;
import com.sun.tools.javac.code.Attribute;
import weaver.conn.RecordSet;
import weaver.formmode.setup.CodeBuild;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 * @author	Huangwm
 * @description	实物资产登记时校验字段以及其他列表的操作api
 */
@Path("/assets/formmode/assetDataCheck")
public class AssetDataCheck {
    private LogTool log = new LogTool("/log/assetlog/huangwm/AssetDataCheck",false);
    private static DBTools dbTools = new DBTools();
    private static DateTools dateTools = new DateTools();
    /**
     * 实物资产登记时检查所关联的关保资产是否重复登记 根据卡片唯一标识
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/checkKpwybs")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> assetDataCheck_kph(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            String kpwybs = "";
            if(requestParams.containsKey("kpwybs")){
                kpwybs = requestParams.get("kpwybs").toString();
                if(kpwybs.equals("")){
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数kpwybs无值,请检查新建页面代码块");
                    return apiResult;
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数kpwybs,请检查新建页面代码块");
                return apiResult;
            }
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            RecordSet rs =  new RecordSet();
            String sql = "select id,swzcbh,zcmc,(select lastname from HrmResource where id=zrr) as zrr,(select departmentname from HrmDepartment where id=syks) as syks,(select subcompanyname from HrmSubCompany where id=dybm) as dybm from uf_swzczmxx where xzgbxtzc='"+kpwybs+"'";
            log.writeLog("checkKpwybs 检查资产唯一标识是否重复->"+sql);
            rs.execute(sql);
            if(rs.next()){
                apiResult.put("status", true);
                //apiResult.put("msg", "当前选择关保资产已被登记,登记人："+rs.getString("zrr")+"/"+rs.getString("syks")+"/"+rs.getString("dybm"));
                apiResult.put("msg", "当前选择关保资产已被登记,实物资产编号:"+rs.getString("swzcbh"));
                return apiResult;
            }
            apiResult.put("status", true);
            apiResult.put("msg", "校验成功,实物表中不存在"+kpwybs+"的重复登记");
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg", "程序异常 "+e.getMessage());
            e.printStackTrace();
        }
        return  apiResult;
    }

    /**
     * 删除关保系统资产数据
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/hideOrShowGbzc")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> hideOrShowGbzc(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            String action = "";
            if(requestParams.containsKey("action")){
                action = requestParams.get("action").toString();
                if(action.equals("")){
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数action无值,请检查查询页代码块");
                    return apiResult;
                }else{
                    if(!action.equals("hide") && !action.equals("show")){
                        apiResult.put("status", false);
                        apiResult.put("msg", "参数action参数值["+action+"]不正确,应为hide或show");
                        return apiResult;
                    }
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数action,请检查查询页代码块");
                return apiResult;
            }
            //SyncAssetsInfoToEcology sync = new SyncAssetsInfoToEcology();
            //sync.start();
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            if(dbTools.checkIsAdmin(userid, "42")){
                //先判断下userid是否在实物资产管理员内
                RecordSet rs =  new RecordSet();
                if(action.equals("hide")) {
                    try {
                        if (rs.execute("update uf_gbxtzczmxx set modedatastatus=1")) {
                            apiResult.put("status", true);
                            apiResult.put("msg", "开启隐藏关保资产数据成功");
                        } else {
                            apiResult.put("status", false);
                            apiResult.put("msg", "开启隐藏关保资产数据失败;请联系关务云管理员检查日志");
                        }
                    }catch (Exception e){
                        apiResult.put("status", false);
                        apiResult.put("errorCode", "-999");
                        apiResult.put("msg", "执行隐藏关保资产数据时sql异常;"+e.getMessage());
                        e.printStackTrace();
                    }
                }else if(action.equals("show")){
                    try {
                        if (rs.execute("update uf_gbxtzczmxx set modedatastatus=0")) {
                            apiResult.put("status", true);
                            apiResult.put("msg", "释放隐藏关保资产数据成功");
                        } else {
                            apiResult.put("status", false);
                            apiResult.put("msg", "释放隐藏关保资产数据失败;请联系关务云管理员检查日志");
                        }
                    }catch (Exception e){
                        apiResult.put("status", false);
                        apiResult.put("errorCode", "-999");
                        apiResult.put("msg", "执行隐藏关保资产数据时sql异常;"+e.getMessage());
                        e.printStackTrace();
                    }
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg","您当前不是系统管理员,无权限修改;如有需要请联系关务云系统管理员授权!");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        ModeComInfo modeComInfo = new ModeComInfo();
        modeComInfo.removeCache();
        return  apiResult;
    }

    /**
     * 帐实对比
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/compareAssetDatas")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> compareAssetDatas(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            String type = "";
            if(requestParams.containsKey("type")){
                type = requestParams.get("type").toString();
                if(type.equals("")){
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数type无值,请检查查询页代码块");
                    return apiResult;
                }else{
                    if(!type.equals("swzc") && !type.equals("gbzc")){
                        apiResult.put("status", false);
                        apiResult.put("msg", "参数type参数值["+type+"]不正确,应为swzc或gbzc");
                        return apiResult;
                    }
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数type,请检查查询页代码块");
                return apiResult;
            }
            //SyncAssetsInfoToEcology sync = new SyncAssetsInfoToEcology();
            //sync.start();
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            if(dbTools.checkIsAdmin(userid, "42")){
                //先判断下userid是否在实物资产管理员内
                RecordSet rs =  new RecordSet();
                if(type.equals("swzc")) {
                    //实物资产对碰
                    try {
                        SwExceptionConjob swCompare = new SwExceptionConjob();
                        swCompare.compareSWToGB();
                        apiResult.put("status", true);
                        apiResult.put("msg", "实物资产对碰实物资产结束,请查看结果;");
                    }catch (Exception e){
                        apiResult.put("status", false);
                        apiResult.put("errorCode", "-999");
                        apiResult.put("msg", "实物资产对碰时程序异常;"+e.getMessage());
                        e.printStackTrace();
                    }
                }else if(type.equals("gbzc")){
                    //关保资产对碰
                    try {
                        GbSwExceptionConjob gbCompare = new GbSwExceptionConjob();
                        gbCompare.compareGBToSW();
                        apiResult.put("status", true);
                        apiResult.put("msg", "关保资产对碰实物资产结束,请查看结果;");
                    }catch (Exception e){
                        apiResult.put("status", false);
                        apiResult.put("errorCode", "-999");
                        apiResult.put("msg", "关保资产对碰时程序异常;"+e.getMessage());
                        e.printStackTrace();
                    }
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg","您当前不是系统管理员,无数据操作权限;如有需要请联系关务云系统管理员授权!");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        ModeComInfo modeComInfo = new ModeComInfo();
        modeComInfo.removeCache();
        return  apiResult;
    }

    /**
     * 封存实物资产列表勾选的数据
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/hideSwzcListDatas")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> HideSwzcListDatas(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            String ids = "";
            if(requestParams.containsKey("ids")){
                ids = requestParams.get("ids").toString();
                if(!ids.equals("")){
                    try {
                        RecordSet rs = new RecordSet();
                        String sql = "update uf_swzczmxx set sjzt=1 where id in("+ids+")";
                        log.writeLog("封存实物资产列表勾选的数据->"+sql);
                        boolean result = rs.execute(sql);
                        if(!result){
                            apiResult.put("status", false);
                            apiResult.put("msg", "更新实物资产数据表封存状态失败,请检查后台代码!");
                        } else{
                            apiResult.put("status", true);
                            apiResult.put("msg", "更新实物资产数据表封存状态成功");
                        }
                    }catch (Exception sqle){
                        apiResult.put("status", false);
                        apiResult.put("msg", "更新实物资产数据表封存状态失败,请检查后台代码! <br />"+ sqle.getMessage());
                    }
                }else{
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数ids无值,请检查封存按钮代码块");
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数ids,请检查封存按钮代码块");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        ModeComInfo modeComInfo = new ModeComInfo();
        modeComInfo.removeCache();
        return  apiResult;
    }

    /**
     * 解封实物资产列表勾选的数据
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/showSwzcListDatas")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> ShowSwzcListDatas(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            String ids = "";
            if(requestParams.containsKey("ids")){
                ids = requestParams.get("ids").toString();
                if(!ids.equals("")){
                    try {
                        RecordSet rs = new RecordSet();
                        String sql = "update uf_swzczmxx set sjzt=0 where id in("+ids+")";
                        log.writeLog("解封实物资产列表勾选的数据->"+sql);
                        boolean result = rs.execute(sql);
                        if(!result){
                            apiResult.put("status", false);
                            apiResult.put("msg", "解封实物资产数据失败,请检查后台代码!");
                        } else{
                            apiResult.put("status", true);
                            apiResult.put("msg", "解封实物资产数据成功");
                        }
                    }catch (Exception sqle){
                        apiResult.put("status", false);
                        apiResult.put("msg", "解封实物资产数据失败,请检查后台代码! <br />"+ sqle.getMessage());
                    }
                }else{
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数ids无值,请检查解封按钮代码块");
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数ids,请检查解封按钮代码块");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        ModeComInfo modeComInfo = new ModeComInfo();
        modeComInfo.removeCache();
        return  apiResult;
    }

    /**
     * 导入生成实物资产
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/importBuildSwzc")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> ImportBuildSwzc(@Context HttpServletRequest request, @Context HttpServletResponse response){
        log.writeLog("=====导入生成实物资产开始=====");
        Map<String, Object> apiResult = new HashMap<String, Object>();
        final int FORMMODEID = 102;		//模块ID
        final int MODEDATACREATERTYPE = 0;	//创建人类型
        final int BILLID = -198;	//表单id
        ModeRightInfo mri = new ModeRightInfo();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            //先判断下userid是否在实物资产管理员内
            if(dbTools.checkIsAdmin(userid, "42")){
                String ids = "";
                String pp_params = ""; //参数里的品牌
                String ggxh_params = "";//参数里的规格型号
                String sblx_params = "";//参数里的设备类型
                if(requestParams.containsKey("ids")){
                    ids = requestParams.get("ids").toString();
                    if(!ids.equals("")){
                        List<String> checks_swzcbh = new  ArrayList<String>();//用于存放已经在登记实物的
                        List<String> checks_id = new  ArrayList<String>();
                        try{
                            RecordSet rs1 = new RecordSet();
                            String checkSw = "select  a.id as swid,swzcbh,kpwybh,b.kph,b.id as gbid from uf_swzczmxx a left join  uf_gbxtzczmxx b on a.xzgbxtzc=b.kpwybs where b.id in("+ids+")";
                            log.writeLog("检查导入时勾选的id是否有关联的："+checkSw);
                            rs1.execute(checkSw);
                            while(rs1.next()){
                                checks_swzcbh.add(rs1.getString("swzcbh"));
                                checks_id.add(rs1.getString("gbid"));
                            }
                        }catch (Exception e){
                            apiResult.put("status", false);
                            apiResult.put("errorCode", "-999");
                            apiResult.put("msg","检查当前勾选的关保资产是否已匹配实物资产异常 "+ e.getMessage());
                            e.printStackTrace();
                            return apiResult;
                        }
                        log.writeLog("checks_id>>>"+checks_id.toString());
                        try{
                            String[] idsIndex =ids.split(",");
                            List<String> index = Arrays.asList(idsIndex);
                            List<String> errorId = new ArrayList<>();
                            List<String> successId = new ArrayList<>();
                            RecordSet rs2 = new RecordSet();
                            RecordSet rs3 = new RecordSet();
                            RecordSet rs4 = new RecordSet();
                            for(int i=0;i<index.size();i++){
                                //数据不为空 且不在已登记的记录当中
                                if(!"".equals(index.get(i)) && !checks_id.contains(index.get(i))){
                                    //先查询关保资产数据 再插入到实物表中
                                    try{
                                        Map<String,String> data = dbTools.getGbzcxx(index.get(i));
                                        String gbid = data.get("id");
                                        String kpwybs = data.get("kpwybs");
                                        String kph = data.get("kph");
                                        String mlmc = data.get("mlmc");
                                        String gdzcmc = data.get("gdzcmc");
                                        String pp = data.get("pp");
                                        String ggxh = data.get("ggxh");
                                        String hth = data.get("hth");
                                        String qdrq = data.get("qdrq");
                                        String dj = data.get("dj");
                                        String sybm = data.get("sybm");
                                        if("".equals(sybm) || "0".equals(sybm)){
                                            log.writeLog("关保数据卡片号["+kph+"]未匹配到使用部门 无法识别产权状况");
                                            errorId.add(index.get(i));
                                            continue;
                                        }else {
                                            //根据sybm获取产权状况
                                            int cqzk = -1;
                                            if(cqzk<0){
                                                log.writeLog("关保数据卡片号["+kph+"]产状状况获取为空");
                                                errorId.add(index.get(i));
                                                continue;
                                            }else{
                                                String uuid = dbTools.getUUID();
                                                if ("".equals(dbTools.getSblxByMldm(mlmc)) || "0".equals(dbTools.getSblxByMldm(mlmc))) {
                                                    errorId.add(index.get(i));
                                                    log.writeLog("关保数据卡片号[" + kph + "]未匹配到实物设备分类");
                                                } else {
                                                    String insertMode = "insert into uf_swzczmxx(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID,zclx,cqzk,sjzt,bjwjcsb) values(";
                                                    insertMode += FORMMODEID + "," + userid + "," + MODEDATACREATERTYPE + ",'" + dateTools.getCurrentDate() + "','" + dateTools.getCurrentTime() + "','" + uuid + "'," + dbTools.getSblxByMldm(mlmc) + "," + cqzk + ",0,0)";
                                                    log.writeLog(insertMode);
                                                    boolean flag = rs2.execute(insertMode);
                                                    if (flag) {
                                                        try {
                                                            String getBillid = "select id from uf_swzczmxx where MODEUUID='" + uuid + "'";
                                                            log.writeLog("根据uuid查询id：" + getBillid);
                                                            rs3.execute(getBillid);
                                                            if (rs3.next()) {
                                                                //生成模块编码 参数为ModeCode表中的编码记录id
                                                                CodeBuild codeBuild = new CodeBuild(2);
                                                                String code = codeBuild.getModeCodeStr(BILLID, rs3.getInt("id"));
                                                                log.writeLog("billid[" + rs3.getInt("id") + "]生成的建模编码为" + code);
                                                                Object[] object = {
                                                                        code, gdzcmc, dj.equals("") ? "0.0" : dj, dbTools.getPp(pp), dbTools.getGgxh(ggxh), 0, qdrq, hth, kpwybs, kph, kpwybs, rs3.getInt("id")
                                                                };
                                                                log.writeLog("插入后更新实物资产信息参数：" + Arrays.asList(object).toString());
                                                                String updateMode = "update uf_swzczmxx set swzcbh=?, zcmc=?,dj=?,pp=?,ggxh=?,zczt=?,rkrq=?,hth=?,kpwybh=?,kph=?,xzgbxtzc=? where id=?";
                                                                rs4.executeUpdate(updateMode, object);
                                                                //权限重构
                                                                mri.editModeDataShare(Integer.parseInt(userid), FORMMODEID, rs3.getInt("id"));

                                                                String updateGb = "update uf_gbxtzczmxx set glswzc=1,dyswzc=" + rs3.getInt("id") + " where id=" + index.get(i);
                                                                boolean updateGBxx = rs4.executeUpdate(updateGb);
                                                                log.writeLog("反更新关保资产关联信息sql：" + updateGb);
                                                                log.writeLog("反更新关保资产关联信息结果：" + updateGBxx);
                                                                successId.add(index.get(i));
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                            errorId.add(index.get(i));
                                                            log.writeLog("id 为" + index.get(i) + "的实物资产导入生产实物资产异常;" + e.getMessage());
                                                        }
                                                    } else {
                                                        errorId.add(index.get(i));
                                                        log.writeLog("关保数据id" + index.get(i) + "插入实物表时sql执行异常");
                                                    }
                                                }
                                            }
                                        }
                                    }catch (Exception e){
                                        log.writeLog("Exception error msg: "+e.getMessage());
                                        e.printStackTrace();
                                    }
                                }
                            }
                            apiResult.put("status",true);
                            String msg = "导入完成,共操作"+index.size()+"条数据;";
                            if(successId.size()>0){
                                msg += "其中"+successId.size()+"条导入成功;";
                            }
                            if(errorId.size()>0){
                                msg += "其中有"+errorId.size()+"条倒入失败;";
                            }
                            if(checks_id.size()>0){
                                msg += "有"+checks_id.size()+"条已经关联实物资产未导入;";
                            }
                            if(errorId.size()>0 || checks_id.size()>0)
                                msg += "请联系管理员查看导入日志！";
                            apiResult.put("msg",msg);
                            log.writeLog("=====导入生成实物资产结束=====");
                        }catch (Exception e){
                            apiResult.put("status", false);
                            apiResult.put("errorCode", "-999");
                            apiResult.put("msg","程序异常 "+ e.getMessage());
                            e.printStackTrace();
                            return apiResult;
                        }

                    }else{
                        apiResult.put("status", false);
                        apiResult.put("msg", "参数ids无值,请检查询页代码块");
                    }
                }else{
                    apiResult.put("status", false);
                    apiResult.put("msg", "缺少参数ids,请检查查询页代码块");
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg","您当前不是系统管理员,无权限操作;如有需要请联系关务云系统管理员授权!");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        return  apiResult;
    }

    /**
     * 获取部门资产概况
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getBmZcgk")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> GetBmZcgk(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            String bmid = "";
            if(requestParams.containsKey("bmid")){
                bmid = requestParams.get("bmid").toString();
                if(!bmid.equals("")){
                    try{

                    }catch (Exception e){
                        apiResult.put("status", false);
                        apiResult.put("errorCode", "-999");
                        apiResult.put("msg","程序异常 "+ e.getMessage());
                        e.printStackTrace();
                    }
                }else{
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数bmid无值,请检流程代码块");
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数bmid,请检查流程代码块");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        return  apiResult;
    }

    /**
     * 根据流程明细中的耗材配件id对比数量是否超出
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/checkHcpjCounts")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> checkHcpjCounts(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID()+"";
            String requestid = "";
            if(requestParams.containsKey("requestid")){
                requestid = requestParams.get("requestid").toString();
                if(!requestid.equals("")){
                    try{
                        RecordSet rs = new RecordSet();
                        List<Map<String,Object>> list = new ArrayList<>();
                        String sql = "select t.*,a.dqsl,(a.dqsl-t.sqsl)as xc from uf_hcpjxx a right join (select m.requestId,c.hcmc,sum(c.sqsl)as sqsl from formtable_main_403_dt1 c left join formtable_main_403 m on m.id=c.mainid " +
                                     " group by requestId,hcmc) t on a.id=t.hcmc where (a.dqsl-t.sqsl)<0 and t.requestId='"+requestid+"'";
                        log.writeLog("checkHcpjCounts-->"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            Map<String,Object> data = new HashMap<>();
                            String hcmc = rs.getString("hcmc");
                            double sqsl = rs.getDouble("sqsl");
                            double dqsl = rs.getDouble("dqsl");
                            double xc = rs.getDouble("xc");
                            data.put("requestid", requestid);
                            data.put("hcmc", hcmc);
                            data.put("sqsl", sqsl);
                            data.put("dqsl", dqsl);
                            data.put("xc", xc);
                            list.add(data);
                        }
                        apiResult.put("status", true);
                        apiResult.put("result", list);
                        apiResult.put("resultLength", list.size());
                    }catch (Exception e){
                        apiResult.put("status", false);
                        apiResult.put("errorCode", "-999");
                        apiResult.put("msg","程序异常 "+ e.getMessage());
                        e.printStackTrace();
                    }
                }else{
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数requestidid无值,请检流程代码块");
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数requestid,请检查流程代码块");
            }

        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        return  apiResult;
    }

    /**
     * 根据设备类型id获取所有对应的品牌id
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getPPidsBySblx")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getAllPpidsBySblx(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID() + "";
            String sblx = "";
            if(requestParams.containsKey("sblx")){
                sblx = requestParams.get("sblx").toString();
                if(!sblx.equals("")){
                    try{
                        RecordSet rs = new RecordSet();
                        List<String> list = new ArrayList<>();
                        String sql = "select id,ppmc,ksysblx from uf_swzcpp ";
                        rs.execute(sql);
                        while (rs.next()){
                            String id = rs.getString("id");
                            String idstr = rs.getString("ksysblx");
                            if(!"".equals(idstr) && !"null".equals(idstr)){
                                //转化为数组进行判断
                                String[] idsIndex = idstr.split(",");
                                if(Arrays.asList(idsIndex).contains(sblx)){
                                    list.add(id);
                                }
                            }
                        }
                        apiResult.put("status", true);
                        apiResult.put("result", list.toString().replace("[","").replace("]",""));
                        apiResult.put("resultLength", list.size());
                    }catch (Exception e){
                        apiResult.put("status", false);
                        apiResult.put("errorCode", "-999");
                        apiResult.put("msg","程序异常 "+ e.getMessage());
                        e.printStackTrace();
                    }
                }else{
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数sblx无值,请检新建布局代码块");
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数sblx,请检查新建流程代码块");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        return apiResult;
    }

    /**
     * 更新耗材领用对应勾选记录为已配送
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/updateHclyjl")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> updateHclyjl(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        log.writeLog("更新耗材领用记录的配送状态开始");
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID() + "";
            Map<String, String> searchDatas = null;
            if (requestParams.containsKey("searchDatas")) {
                searchDatas = JSONObject.parseObject(requestParams.get("searchDatas").toString(), HashMap.class);
                log.writeLog(searchDatas);
                String con_14711 = searchDatas.get("con_14711");//申请单位
                log.writeLog("con_14711>>>" + con_14711);
                String con_14712 = searchDatas.get("con_14712");//申请日期类型
                log.writeLog("con_14712>>>" + con_14712);
                String con_14714 = searchDatas.get("con_14714");//耗材类型
                log.writeLog("con_14714>>>" + con_14714);
                String con_14715 = searchDatas.get("con_14715");//打印机品牌
                log.writeLog("con_14715>>>" + con_14715);
                String con_14716 = searchDatas.get("con_14716");//规格型号
                log.writeLog("con_14716>>>" + con_14716);

                //查询需要更新的数据
                RecordSet rs = new RecordSet();
                String sqlwhere = "where 1=1";
                //日期需要分解
                /**
                 * dateType
                 * 0 全部 则条件不增加申请日期判断
                 * 9 昨天 获取当前日期的上一天
                 * 1 今天 获取当前日期
                 * 10 明天 获取当前日期的前一天
                 * 11 上周 获取上周的所有日期
                 * 2  本周 获取本周的所有日期
                 * 12 下周 获取下周的所有日期
                 * 3 本月 则判断申请日期为当前月份
                 * 4 本季度 获取当前季度的月份
                 * 5 本年 则判断申请日期为本年
                 * 7 上个月 则判断申请日期为上个月份
                 * 8 上年 则判断申请日期为上年份
                 * 6 具体日期 则获取数组的第二位和第三位作为开始和结束日期的区间
                 */
                //只保留0 1 3 7 6 的条件
                if (!con_14712.equals("0")) {
                    if (con_14712.equals("1")) {
                        sqlwhere += "and sqrq='" + dateTools.getCurrentDate() + "'";
                    } else if (con_14712.equals("3")) {
                        sqlwhere += "and MMonth(sqrq)=Month(GETDATE())";
                    } else if (con_14712.equals("7")) {
                        sqlwhere += "and MMonth(sqrq)=Month(GETDATE())-1";
                    } else if (con_14712.equals("6")) {
                        String startDate = searchDatas.get("startdate");
                        log.writeLog("startDate>>>" + startDate);
                        String endDate = searchDatas.get("enddate");
                        log.writeLog("endDate>>>" + endDate);
                        sqlwhere += " and sqrq between '" + startDate + "' and '" + endDate + "'";
                    }
                }
                if (!"".equals(con_14711)) sqlwhere += " and sqdw=" + con_14711;
                if (!"".equals(con_14714)) sqlwhere += " and hclx=" + con_14714;
                if (!"".equals(con_14715)) sqlwhere += " and dyjpp=" + con_14715;
                if (!"".equals(con_14716)) sqlwhere += " and ggxh=" + con_14716;

                String sql = "select * from uf_hclyjl " + sqlwhere;
                String updatesql = "update uf_hclyjl set pszt=1 " + sqlwhere;
                boolean isUpdate = rs.execute(updatesql);
                log.writeLog(sql);
                apiResult.put("status", isUpdate);
                apiResult.put("userid", userid);
                apiResult.put("searchDatas", searchDatas);
            } else {
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数searchDatas,请检查ecode代码");
            }

        } catch (Exception e) {
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg", "程序异常 " + e.getMessage());
            e.printStackTrace();
        }
        log.writeLog("更新耗材领用记录的配送状态结束");
        return apiResult;
    }

    /**
     * 往内部控制明细表插入数据
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/inisertToNeiBuKongZhi")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> inisertToNeiBuKongZhi(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("weaver_user@bean");
            String userid = user.getUID() + "";
            String time = dateTools.getCurrentTimes2();
            String id = "";
            String value = "";
            if(requestParams.containsKey("id")){
                id = requestParams.get("id").toString();
                if(!id.equals("")){
                    RecordSet rs = new RecordSet();
                    if(requestParams.containsKey("value")){
                        value  = requestParams.get("value").toString();
                        if(!"".equals(value)){
                            try {
                                String sql = "insert into uf_risk_bill_dt1(mainid,lsqk,lrry,fbsj)values("+id+",'"+value+"','"+userid+"','"+time+"')";
                                boolean flag = rs.execute(sql);
                                apiResult.put("status", flag);
                                apiResult.put("msg", flag==true?"sql插入成功":"sql插入失败:"+sql);
                            }catch (Exception e){
                                apiResult.put("status", false);
                                apiResult.put("errorCode", "-999");
                                apiResult.put("msg","程序异常 "+ e.getMessage());
                                e.printStackTrace();
                            }
                        }else{
                            apiResult.put("status", false);
                            apiResult.put("msg", "参数id无值,请检查查询页代码块");
                        }
                    }else{
                        apiResult.put("status", false);
                        apiResult.put("msg", "缺少参数value,请检查查询页代码块");
                    }
                }else{
                    apiResult.put("status", false);
                    apiResult.put("msg", "参数id无值,请检查查询页代码块");
                }
            }else{
                apiResult.put("status", false);
                apiResult.put("msg", "缺少参数id,请检查查询页代码块");
            }
        }catch (Exception e){
            apiResult.put("status", false);
            apiResult.put("errorCode", "-999");
            apiResult.put("msg","程序异常 "+ e.getMessage());
            e.printStackTrace();
        }
        return apiResult;
    }
}
